var months = new Array( 'January', 'February', 'March', 'April', 'May',
                        'June', 'July', 'August', 'September', 'October',
                        'November', 'December');
var timeAct = new Date();

$(function(){
    $.datepicker.setDefaults($.datepicker.regional['en-GB']);

    //--------------------------------------------------------------------------
    // year slider
    var yearSliderAct = new Date();
    var yearSliderMin = new Date($('#yearSliderMin').val()*1000);
    //var yearSliderMax = new Date($('#yearSliderMax').val()*1000);
    
    $("#yearSlider").slider({
        value: 0,
        min: 0,
        max: yearSliderAct.getFullYear() - yearSliderMin.getFullYear(),
        step: 1,
        slide: function( event, ui ) {
            var year = timeAct.getFullYear() - ui.value;
            var month0 = $("#monthSlider").slider("values", 0);
            var month1 = $("#monthSlider").slider("values", 1);
            $("#sliderVal").html(year + " " + months[month0] + " - " + months[month1]);
        }
    });
    $("#yearSlider").bind("slidestop", function() {
        sliderValueChanged();
    });
    
    //--------------------------------------------------------------------------
    // month slider    
    $("#monthSlider").slider({
        range: true,
        min: 0,
        max: 11,
        values: [0,11],
        step: 1,
        slide: function( event, ui ) {
            var year = timeAct.getFullYear() - $("#yearSlider").slider("option", "value");
            var month0 = ui.values[0];
            var month1 = ui.values[1];
            $("#sliderVal").html(year + " " + months[month0] + " - " + months[month1]);
        }
    });
    $("#monthSlider").bind("slidestop", function() {
        sliderValueChanged();
    });
    
    // set actual month and +1
    $("#monthSlider").slider("values", 0, yearSliderAct.getMonth());
    $("#monthSlider").slider("values", 1, yearSliderAct.getMonth()+1);
    
    var year = timeAct.getFullYear() - $("#yearSlider").slider("option", "value");
    var month0 = $("#monthSlider").slider("values", 0);
    var month1 = $("#monthSlider").slider("values", 1);
    $("#sliderVal").html(year + " " + months[month0] + " - " + months[month1]);
    
    // when page loaded, render events
    sliderValueChanged(true);

    //--------------------------------------------------------------------------
    // modal window
    $('#modalAddEvent').modal({
        keyboard: true,
        backdrop: true
    });
    $('#modalAddEvent').bind("shown", function() {
        $("#formAddNewEventErrors").html("");
        $('#formAddNewEvent #name').val("");
        $('#formAddNewEvent #type').val("");
        $('#formAddNewEvent #timeStart').val("");
        $('#formAddNewEvent #timeEnd').val("");
        
        $("#formAddNewEvent ul.alert-message.error-for-ketchup").hide();
    });

    formAddNewEventValidate();
    formAddNewEventDateRangePicker();
    
    //--------------------------------------------------------------------------
    // Work states (read, edit, trash)
    $("#stateWorkReadLink").bind('click', function() {
        if ($("#stateWorkReadLink>img").hasClass('stateWorkInactive')) {
            $("#stateWorkReadLink>img").removeClass('stateWorkInactive');
            $("#stateWorkEditLink>img").addClass('stateWorkInactive');
            $("#stateWorkTrashLink>img").addClass('stateWorkInactive');
        }
        
        return false;
    });
    $("#stateWorkEditLink").bind('click', function() {
        if ($("#stateWorkEditLink>img").hasClass('stateWorkInactive')) {
            $("#stateWorkEditLink>img").removeClass('stateWorkInactive');
            $("#stateWorkReadLink>img").addClass('stateWorkInactive');
            $("#stateWorkTrashLink>img").addClass('stateWorkInactive');
        }
        
        return false;
    });
    $("#stateWorkTrashLink").bind('click', function() {
        if ($("#stateWorkTrashLink>img").hasClass('stateWorkInactive')) {
            $("#stateWorkTrashLink>img").removeClass('stateWorkInactive');
            $("#stateWorkReadLink>img").addClass('stateWorkInactive');
            $("#stateWorkEditLink>img").addClass('stateWorkInactive');
        }
        
        return false;
    });
});

function getWorkState() {
    if ( ! $("#stateWorkEditLink>img").hasClass('stateWorkInactive'))
        return 'edit';
    else if ( ! $("#stateWorkTrashLink>img").hasClass('stateWorkInactive'))
        return 'trash';
    return 'read';
}